Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loans: Runtime API for updating the portfolio with fake prices #1748

Merged
merged 2 commits into from
Feb 29, 2024

Conversation

lemunozm
Copy link
Contributor

Description

Add a Runtime API to emulate a portfolio update with custom prices.

@lemunozm lemunozm added Q3-medium Can be done with good experience in the language, but little knowledge of the codebase. P7-asap Issue should be addressed in the next days. I8-enhancement An additional feature. I6-refactoring Code needs refactoring. crcl-protocol Circle protocol related. labels Feb 28, 2024
@lemunozm lemunozm self-assigned this Feb 28, 2024
@lemunozm
Copy link
Contributor Author

Blocked by #1739

Comment on lines +1082 to +1084
PriceCollectionInput::Empty => BTreeMap::default(),
PriceCollectionInput::Custom(prices) => prices.into(),
PriceCollectionInput::FromRegistry => Self::registered_prices(pool_id)?,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the benfit of using the PriceCollectionInput here vs just using a BTreeMap as the parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@lemunozm lemunozm Feb 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty is just sugar for Custom([]) and could be removed. But the caller should be able to choose between Custom and FromRegistry.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer @lemunozm's proposal over just using a BTreeMap as it is more explicit which is always helpful for runtime API which can be expected to be used by externals as well.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, you changed the valuation parameters of the runtime-api. Sounds good!

The most important thing though would be to mimic closing an epoch with

  • faked prices
  • Optional

Returning:

  • Fullfillment amounts for each tranche for both invest and redeem
  • New pool-reserve
  • New Tranche prices

Copy link
Contributor Author

@lemunozm lemunozm Feb 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I guess somehow this should be amalgamated with some other pallet-pool-system calls to get this, right?

Then I propose to create another in the PoolSystem RuntimeAPI, maybe called simulate_close_epoch or similar, where all these calls are performed to return all the required values above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, I'll do in another PR pool-related.

Copy link
Contributor

@wischli wischli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really clean runtime API! I have a question before I hit the approval button.

Comment on lines +1082 to +1084
PriceCollectionInput::Empty => BTreeMap::default(),
PriceCollectionInput::Custom(prices) => prices.into(),
PriceCollectionInput::FromRegistry => Self::registered_prices(pool_id)?,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer @lemunozm's proposal over just using a BTreeMap as it is more explicit which is always helpful for runtime API which can be expected to be used by externals as well.

@@ -1208,7 +1236,8 @@ pub mod pallet {
}

fn update_nav(pool_id: T::PoolId) -> Result<T::Balance, DispatchError> {
Ok(Self::update_portfolio_valuation_for_pool(pool_id)?.0)
Self::update_portfolio_valuation_for_pool(pool_id, PriceCollectionInput::FromRegistry)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A quick comprehension question: Our rather newer NAV calculation runtime API calls Loans::update_nav(pool_id) such that the Pool Fees NAV calculation can be based on that. Say the loans NAV is updated with a price collection input in block i and in the next block i+1 we are querying the NAV via PoolsApi::nav(pool_id) which performs Loans::update_nav(pool_id) with an update based on PriceCollectionInput::FromRegistry. IIUC, the prices from the update in block i are still cached. What I want to find out is if Loans::update_nav can make the Loans NAV less accurate if it was previously based on a custom price list.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was previously based on the price registry, so both lines above are exactly identical. From a pool fees perspective, nothing has changed. The only open door (right now) is for the RuntimeAPI.

mustermeiszer
mustermeiszer previously approved these changes Feb 29, 2024
Copy link
Collaborator

@mustermeiszer mustermeiszer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Can you ping apps that the runtime api changed?

@mustermeiszer mustermeiszer added the D5-breaks-api Pull request changes public api. Document changes publicly. label Feb 29, 2024
Copy link
Contributor

@wischli wischli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor nit change I would like to see: Runtime API can be versioned via #[api_version($VERSION)] as part of the decl_runtime_apis! macro (example). However, we have not made use of this feature so far. Of course, we should to make the breaking changes explicit.

@lemunozm
Copy link
Contributor Author

Good point. Just one question: should we bump up the version even if the upgrade is retro-compatible? (it means just an addition)

@lemunozm
Copy link
Contributor Author

@mustermeiszer can we merge #1739 before?

Base automatically changed from loans/linear-accrual-price to main February 29, 2024 10:44
@lemunozm lemunozm dismissed mustermeiszer’s stale review February 29, 2024 10:44

The base branch was changed.

mustermeiszer
mustermeiszer previously approved these changes Feb 29, 2024
@wischli
Copy link
Contributor

wischli commented Feb 29, 2024

Good point. Just one question: should we bump up the version even if the upgrade is retro-compatible? (it means just an addition)

Definitely not a blocker because it's not breaking. In the future, I would still like to make real use of versioning because it can be tied to the earliest supported spec version for that API.

For instance, SubQuery recently had issues with the new PoolsNAV API because it was attempted at blocks which didn't support it. But in order for this to fully work, Apps needs to add spec version granularity to the type and RtApi decorations.

wischli
wischli previously approved these changes Feb 29, 2024
@lemunozm lemunozm dismissed stale reviews from wischli and mustermeiszer via 24adcea February 29, 2024 11:32
@lemunozm
Copy link
Contributor Author

Good reasoning, you've convince me! => 24adcea

@lemunozm lemunozm enabled auto-merge (squash) February 29, 2024 12:56
@lemunozm lemunozm merged commit 10aed20 into main Feb 29, 2024
9 checks passed
@wischli wischli deleted the loans/fake-prices-api branch February 29, 2024 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crcl-protocol Circle protocol related. D5-breaks-api Pull request changes public api. Document changes publicly. I6-refactoring Code needs refactoring. I8-enhancement An additional feature. P7-asap Issue should be addressed in the next days. Q3-medium Can be done with good experience in the language, but little knowledge of the codebase.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants